package ace.module.captcha.core.impl.properties;

import com.alicp.jetcache.anno.CacheType;
import java.time.Duration;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;

/**
 * module properties.
 *
 * @author Caspar
 */
@RefreshScope
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = CaptchaCoreProperties.CONFIG_PREFIX)
public class CaptchaCoreProperties {

  public static final String CONFIG_PREFIX = "ace.captcha";

  /**
   * 验证码默认配置
   */
  private CaptchaItemConfigProperties defaultCaptchaItemConfig =
      CaptchaItemConfigProperties.builder().build();

  /**
   * 验证码 缓存数据仓库实现的缓存配置
   */
  private CaptchaItemOnlyCacheRepositoryImplProperties
      captchaItemOnlyCacheRepositoryImplProperties =
      CaptchaItemOnlyCacheRepositoryImplProperties.builder().build();

  @Data
  @Builder
  @NoArgsConstructor
  @AllArgsConstructor
  public static class CaptchaItemConfigProperties {

    /**
     * 是否启用
     */
    @Builder.Default
    private Boolean enable = true;

    /**
     * 开启系统验证码
     */
    @Builder.Default
    private Boolean enableSystemCaptcha = false;

    /**
     * 系统验证码
     */
    @Builder.Default
    private String systemCaptcha = "8888";

    /**
     * 尝试次数
     */
    @Builder.Default
    private Integer attemptCount = 5;

    /**
     * 过期时间,单位时间-秒
     */
    @Builder.Default
    private Long expireInSeconds = java.time.Duration.ofMinutes(5).toSeconds();
  }

  /**
   * 缓存配置
   */
  @Data
  @Builder
  @NoArgsConstructor
  @AllArgsConstructor
  public static class CaptchaItemOnlyCacheRepositoryImplProperties {

    @Builder.Default
    private Duration localExpire = Duration.ofHours(1);
    @Builder.Default
    private Duration expire = Duration.ofHours(1);
    @Builder.Default
    private CacheType cacheType = CacheType.BOTH;
    @Builder.Default
    private Boolean penetrationProtect = true;
    @Builder.Default
    private Duration penetrationProtectTimeout = Duration.ofSeconds(10);
  }
}
